package com.ruoyi.system.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.system.dl.DeleteImages;
import com.ruoyi.system.domain.Images;
import com.ruoyi.system.mapper.ImagesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.RoomMapper;
import com.ruoyi.system.domain.Room;
import com.ruoyi.system.service.IRoomService;

/**
 * 酒店房间Service业务层处理
 *
 * @author ruoyi
 * @date 2023-05-10
 */
@Service
public class RoomServiceImpl implements IRoomService {
    @Autowired
    private RoomMapper roomMapper;

    @Autowired
    private ImagesMapper imagesMapper;

    /**
     * 查询酒店房间
     *
     * @param id 酒店房间主键
     * @return 酒店房间
     */
    @Override
    public Room selectRoomById(Long id) {
        Room room = roomMapper.selectRoomById(id);
        room.setRoomImages(imagesMapper.selectImages("room"+id));
        return room;
    }

    /**
     * 查询酒店房间列表
     *
     * @param room 酒店房间
     * @return 酒店房间
     */
    @Override
    public List<Room> selectRoomList(Room room) {
        List<Room> rooms = roomMapper.selectRoomList(room);
        List<Room> roomList = rooms.stream().peek(
                rom -> {
                    rom.setRoomImages(imagesMapper.selectImages("room" + rom.getId()));
                }
        ).collect(Collectors.toList());
        return roomList;
    }

    /**
     * 新增酒店房间
     *
     * @param room 酒店房间
     * @return 结果
     */
    @Override
    public int insertRoom(Room room) {
        //新增酒店房间
        int i = roomMapper.insertRoom(room);
        //新增之后 根据输入的信息查询酒店房间信息 获取到当前房间的id
        List<Room> rooms = roomMapper.selectRoomList(room);
        Long roomId = rooms.get(0).getId();

        //拿到输入的图片名称集合
        List<String> roomImages = room.getRoomImages();
        //利用stream 流 将拿到的图片名称都放进images 关联表中
        roomImages.stream().map(map -> imagesMapper.insertSelective(new Images(null, "room" + roomId, map))).collect(Collectors.toList());
        return i;
    }

    /**
     * 修改酒店房间
     *
     * @param room 酒店房间
     * @return 结果
     */
    @Override
    public int updateRoom(Room room) {
        //修改酒店内容  不包括图片
        int i = roomMapper.updateRoom(room);
        //删除与当前房间相关的图片
        imagesMapper.deleteByForeginId("room"+room.getId());
        DeleteImages.deleteIma("room"+room.getId(),imagesMapper);

        //重新将新的图片地址添加到关联表
        List<String> roomImages = room.getRoomImages();
        //方法一
        roomImages.stream().forEach(roomImg->imagesMapper.insert(new Images(null,"room"+room.getId(),roomImg)));
        //方法二
//        for (String roomImg : roomImages) {
//            imagesMapper.insert(new Images(null, "room" + room.getId(), roomImg));
//        }
        return i;
    }

    /**
     * 批量删除酒店房间
     *
     * @param ids 需要删除的酒店房间主键
     * @return 结果
     */
    @Override
    public int deleteRoomByIds(Long[] ids) {
        Arrays.stream(ids).forEach(id -> {
            imagesMapper.deleteByForeginId("room"+id);
            DeleteImages.deleteIma("room"+id,imagesMapper);
        });
        return roomMapper.deleteRoomByIds(ids);
    }

    /**
     * 删除酒店房间信息
     *
     * @param id 酒店房间主键
     * @return 结果
     */
    @Override
    public int deleteRoomById(Long id) {
        imagesMapper.deleteByForeginId("room"+id);
        DeleteImages.deleteIma("room"+id,imagesMapper);
        return roomMapper.deleteRoomById(id);
    }
}
